package com.dycx.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.context.UserInfoContext;
import com.dycx.food.dto.ShoppingCartDTO;
import com.dycx.food.entity.GoodsDO;
import com.dycx.food.entity.ShoppingCartDO;
import com.dycx.food.mapper.ShoppingCartFoodMapper;
import com.dycx.food.service.GoodsFoodService;
import com.dycx.food.service.ShoppingCartFoodService;
import com.dycx.food.vo.ShoppingCartVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@AllArgsConstructor
public class ShoppingCartFoodImpl extends ServiceImpl<ShoppingCartFoodMapper, ShoppingCartDO> implements ShoppingCartFoodService {

    private ShoppingCartFoodMapper shoppingCartMapper;
    private GoodsFoodService goodsService;

    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean add(ShoppingCartDTO dto) {
        //查询库存
        GoodsDO goodsDO = goodsService.getById(dto.getGoodsId());

        LambdaQueryWrapper<ShoppingCartDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartDO::getUid, UserInfoContext.get().getUserId());
        queryWrapper.eq(ShoppingCartDO::getGoodsId, dto.getGoodsId());

        if (dto.getGoodsNum() > goodsDO.getGoodsStorage()) {
            throw new RuntimeException("抱歉，库存不足");
        }

        if (goodsDO.getGoodsPrice() == 0) {
            throw new RuntimeException("商家未设置商品价格，暂不支持购买");
        }

        ShoppingCartDO shoppingCartDO = new ShoppingCartDO();
        shoppingCartDO.setUid(UserInfoContext.get().getUserId());
        shoppingCartDO.setGoodsNum(dto.getGoodsNum());
        shoppingCartDO.setGoodsId(dto.getGoodsId());
        shoppingCartDO.setRemark(dto.getRemark());

        if (!this.save(shoppingCartDO)) {
            throw new RuntimeException("添加购物车失败");
        }

        return true;
    }

    /**
     * 修改购物车商品数量，如果数量为0则删除
     *
     * @param id
     * @return
     */
    @Override
    public Boolean minus(Integer id) {
        ShoppingCartDO shoppingCartInfo = this.getById(id);
        if (!UserInfoContext.get().getUserId().equals(shoppingCartInfo.getUid())) {
            throw new RuntimeException("无权操作当前信息");
        }

        int newNum = shoppingCartInfo.getGoodsNum() - 1;

        if (newNum > 0) {
            shoppingCartInfo.setGoodsNum(newNum);

            if (!this.updateById(shoppingCartInfo)) {
                throw new RuntimeException("修改购物车商品数量失败");
            }
        } else {
            this.removeById(shoppingCartInfo.getId());
        }

        return true;
    }

    /**
     * 购物车添加数量
     *
     * @param id
     * @return
     */
    @Override
    public Boolean plus(Integer id) {
        ShoppingCartDO shoppingCartInfo = this.getById(id);
        if (!UserInfoContext.get().getUserId().equals(shoppingCartInfo.getUid())) {
            throw new RuntimeException("无权操作当前信息");
        }

        int newNum = shoppingCartInfo.getGoodsNum() + 1;
        shoppingCartInfo.setGoodsNum(newNum);
        if (!this.updateById(shoppingCartInfo)) {
            throw new RuntimeException("修改购物车商品数量失败");
        }

        return true;
    }

    /**
     * 获取购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCartVO> getList() {
        return shoppingCartMapper.getList(UserInfoContext.get().getUserId());
    }

    /**
     * 获取购物车列表
     *
     * @return
     */
    @Override
    public List<ShoppingCartVO> getListByIds(List<Integer> ids) {
        return shoppingCartMapper.getListByIds(UserInfoContext.get().getUserId(), ids);
    }

    @Override
    public Boolean clearShoppingCart() {
        LambdaQueryWrapper<ShoppingCartDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartDO::getUid, UserInfoContext.get().getUserId());

        return this.remove(queryWrapper);
    }

    @Override
    public Boolean delCart(Integer id) {
        ShoppingCartDO shoppingCartDO = this.getById(id);
        if (!UserInfoContext.get().getUserId().equals(shoppingCartDO.getUid())) {
            throw new RuntimeException("没有对此数据的操作权限");
        }

        return this.removeById(id);
    }

    @Override
    public Boolean delCartByIds(List<Integer> ids) {
        List<ShoppingCartVO> list = this.getListByIds(ids);
        list.forEach(item -> {
            if (!UserInfoContext.get().getUserId().equals(item.getUid())) {
                throw new RuntimeException("没有对此数据的操作权限");
            }
        });

        return this.removeByIds(ids);
    }

    @Override
    public int countShoppingCart() {
        LambdaQueryWrapper<ShoppingCartDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCartDO::getUid, UserInfoContext.get().getUserId());

        return this.count(queryWrapper);
    }
}
